#!/usr/bin/python

#delete ligands from score table


import sys, string

score_file = open(sys.argv[1], 'r')
ligand_list = open(sys.argv[2], 'r')


out_file = open (sys.argv[3], 'w') 

#----------------------
def score_sort(a,b):
   if a[1] < b[1] :
     return -1
   elif a[1] == b[1]:
     return 0
   else:
     return 1

     

#----------------------

#read labels of ligands that will be deleted
delete_list = []
for i in ligand_list.readlines():
	number = i.strip()
   	new_name = 'V' + number.rjust(8)
   	new_name = new_name.replace(' ', '0')

	delete_list.append(new_name)
ligand_list.close()



dict = {}

total_list = []

for i in score_file.readlines():
    i = i.strip()
    name, rank, score, ele, vdw, polsol, apolsol = i.split('\t')
  
    if name not in delete_list:
    	dict[name] = [float(score), float(vdw),float(ele),float(polsol),float(apolsol)]
	
score_file.close()
  
content = []

#print dict.keys()

items = dict.items()
items.sort(score_sort)
counter = 1
for i in items:
  dict[i[0]].append(counter)
  counter = counter + 1
  
  
content = dict.keys()

content.sort()

for i in content:
  out_file.write(i + '\t' + str(dict[i][5])  + '\t' + '%.3f'%dict[i][0] + '\t' + '%.3f'%dict[i][2] + '\t' + '%.3f'%dict[i][1] + '\t' + '%.3f'%dict[i][3] + '\t' + '%.3f'%dict[i][4] + '\n')
out_file.close()




